﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.Windows.Forms;

//UP主助手插件开发SDK
//怎么开始？很简单，下面有备注~除非你看不懂中文
//请把生成的dll文件放在软件根目录下的“Addons”文件夹里面哦
//Properties/Assemblyinfo.cs是插件的信息，写过C#程序的都知道该如何写了！

namespace 点歌防沉闷//<--命名空间必须与你的文件名相同，比如：test.dll的命名空间必须为test
{
    public class plugin//<--这里不能改
    {
        FileStream fff = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "mlog.log");
        #region 这个是软件调用的部分，请勿阻塞或者使用消息框。
        /// <summary>
        /// 软件主界面打开后触发start
        /// </summary>
        public void start()
        {
            IsPlaying = true;//默认需要用户播放音乐
        }

        /// <summary>
        /// 软件关闭时触发stop
        /// </summary>
        public void stop()
        {

        }

        /// <summary>
        /// 弹幕来了触发dmshow
        /// </summary>
        /// <param name="logtext">弹幕信息</param>
        /// <param name="uid">用户ID</param>
        /// <param name="nickname">用户昵称</param>
        /// <param name="datetime">接收时间(弹幕接收方式2显示为“发送时间：xxx”)</param>
        /// <param name="admin">是否房管（格式：“房管：0或1”）</param>
        /// <param name="member">是否成员</param>
        /// <param name="vip">是否VIP老爷（格式：“会员：0或1”）</param>
        /// <param name="roomid">房间ID</param>
        /// <param name="extra_info">额外信息</param>
        public void dmshow(string logtext,string uid, string nickname, string datetime, string admin, string member, string vip, string roomid, string extra_info)
        {
            if (logtext.IndexOf("去掉重音")>-1 && admin=="房管：1")
            {
                //暂停外部音乐
                if (!IsPlaying)
                {
                    PressPlayKey();
                    IsPlaying = false;
                }
            }
            if (logtext.IndexOf("#投票切歌#")>=0 && CanVote)
            {
                Vote(nickname);
            }
            if (logtext.IndexOf("#取消投票#") >= 0 && CanVote)
            {
                CancleVote(nickname);
            }
        }

        /// <summary>
        /// 主程序会分享一些行为信息给插件，方便插件开发者知道主程序在干什么
        /// P.S.新的特性都会发送记录到这里
        /// </summary>
        /// <param name="logmsg"></param>
        public void ActionLog(string logmsg)
        {
            if (logmsg.IndexOf("已清空")>0)
            {
                Thread p_th = new Thread(Play);
                p_th.Start();
            }
            if (logmsg.IndexOf("水友点歌")>-1)
            {
                //样式："[点歌]水友点歌：东京不太热"
                dglog = logmsg.Remove(0,9);
            }
            if (logmsg.IndexOf("正在播放") >0)
            {
                LogMusicList(dglog);
                //暂停
                if (IsPlaying)
                {
                    PressPlayKey();
                    IsPlaying = !IsPlaying;
                }
            }
            if (logmsg.IndexOf("正在切换音乐") >0)
            {
                //取消当前的投票
                if (InProccess)
                {
                    CancleVote("system!@#$%^&*()");
                }
            }
        }
        #endregion

        /// <summary>
        /// 在插件管理器双击插件会触发admin
        /// </summary>
        public void admin()
        {
            AboutBox1 abb = new AboutBox1(CanVote,UseOtherKey);
            abb.sres += abb_sres;
            abb.Show();
        }

        void abb_sres(bool IsRPL, bool CanVote, bool UseOtherKey)
        {
            this.IsRPL = IsRPL;
            this.CanVote = CanVote;
            this.UseOtherKey = UseOtherKey;
        }
        private void PressPlayKey()
        {
            if (UseOtherKey)
            {
                KeyHook.Play2();
            }
            else
            {
                KeyHook.Play();
            }
        }
        public bool IsRPL = false;
        public bool CanVote = false;
        public string dglog = string.Empty;
        public bool UseOtherKey = false;
        private void LogMusicList(string log)
        {
            
            
            StreamWriter sw = new StreamWriter(fff);
            
            sw.WriteLine(log);
            sw.Flush();
        }

        #region 投票部分
        private bool InProccess = false;
        List<string> VoterList = new List<string>();
        string fqz = string.Empty;
        private void Vote(string username)
        {
            //检查是否发起者
            if (!InProccess)
            {
                fqz = username;
                SendMSG(fqz + "发起切歌，收到3票后即可切歌");
                SendMSG(fqz + "也可以输入#取消投票#来取消当前操作");
            }
            InProccess = true;
            //不许重复投票
            if (VoterList.Contains(username))
            {
                return;
            }
            //增加票数
            VoterList.Add(username);
            //点票
            if (VoterList.Count>=3)
            {
                SendMSG("UP主助手-点歌防沉闷.Admin@main_ChangeMusic;", true);
                InProccess = false;
                fqz = string.Empty;
                VoterList.Clear();
            }
        }
        private void CancleVote(string username)
        {
            if (fqz==username||username=="system!@#$%^&*()")
            {
                InProccess = false;
                fqz = string.Empty;
                VoterList.Clear();
                SendMSG("投票切割已取消！");
            }
        }
        #endregion

        #region 控制外部播放器
        private bool IsPlaying = false;//外部播放器是否在播放
        private void Play()
        {
            if (!IsPlaying && !IsRPL)
            {
                PressPlayKey();
                IsPlaying = true;
            }
            else if (!IsPlaying&& IsRPL)
            {
                //智能点歌
                StreamReader sr = new StreamReader(fff);
                string now ="";

                while (sr.Peek() >= 0)
                {
                    now = sr.ReadLine();
                    SendMSG("233 " + now);
                }
                sr.Close();
            }
        }
        #endregion


        #region 这块就不需要动了，除非你想研究
        public event EventHandler SMSG;
        private void SendMSG(string text,bool action=false)
        {
            string _text = "{dl_title}";
            if (action)
            {
                _text = "{action}";
            }
            SMSG(new object[] { _text + text }, null);
        }
        #endregion
    }
}
